﻿using Common.Contract.Customer.Identifier;
using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.Provider
{
    public class DistrictProvider : GenericProvider, IDistrictProvider
    {
        public List<District> SearchDistricts(out int totalRecord, string textSearch = null, int pagesize = 0, int pageindex = 0)
        {
            var searcher = (textSearch ?? string.Empty).ToUpper();
            var querry = repository.GetQuery<District>().Include(x => x.Province);
            if (DistrictIdentifier.IsValid(textSearch))
            {
                DistrictIdentifier identifier = new DistrictIdentifier(textSearch);
                int id = identifier.PersistableID;
                querry = querry.Where(x => x.Id == id);
            }
            else
                querry = querry.Where(x => x.Name.ToUpper().Contains(searcher));

            querry = querry.OrderBy(x => x.Id);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public List<District> SearchDistricts(out int totalRecord, string districtIdentifier, string DistrictName,
            decimal? lng, decimal? lat, string provinceIdentifier,
            int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<District>().Include(x => x.Province);
            if (DistrictIdentifier.IsValid(districtIdentifier))
            {
                DistrictIdentifier identifier = new DistrictIdentifier(districtIdentifier);
                int id = identifier.PersistableID;
                querry = querry.Where(x => x.Id == id);
            }

            if (!string.IsNullOrWhiteSpace(DistrictName))
                querry = querry.Where(x => x.Name.ToUpper().Contains(DistrictName.ToUpper()));

            if (lng.HasValue)
                querry = querry.Where(x => x.Lng == lng);

            if (lng.HasValue)
                querry = querry.Where(x => x.Lat == lat);

            if (!string.IsNullOrWhiteSpace(provinceIdentifier) && ProvinceIdentifier.IsValid(provinceIdentifier))
            {
                var identifierPro = new ProvinceIdentifier(provinceIdentifier);
                var idPro = identifierPro.PersistableID;
                querry = querry.Where(x => x.ProvinceId == idPro);
            }

            querry = querry.OrderBy(x => x.Id);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public District RetrieveDistrict(string districtIdentifier)
        {
            if (DistrictIdentifier.IsValid(districtIdentifier))
            {
                DistrictIdentifier identifier = new DistrictIdentifier(districtIdentifier);
                int id = identifier.PersistableID;
                var querry = repository.GetQuery<District>(x => x.Id == id).Include(x => x.Province);
                return querry.FirstOrDefault();
            }
            return null;
        }

        public bool Add(District record)
        {
            try
            {
                repository.Add<District>(record);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(District record)
        {
            try
            {
                var updateRecord = repository.FindOne<District>(x => x.Id == record.Id);
                if (updateRecord == null) return false;
                updateRecord.Name = record.Name;
                updateRecord.Lat = record.Lat;
                updateRecord.Lng = record.Lng;
                updateRecord.ProvinceId = record.ProvinceId;
                updateRecord.UpdateUserId = record.UpdateUserId;
                updateRecord.UpdateUserName = record.UpdateUserName;
                updateRecord.UpdateDateTime = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                repository.Delete<District>(x => x.Id == id);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
